import { defineComponent, ref, watch, nextTick, onMounted, onBeforeUnmount, openBlock, createElementBlock, createVNode, unref, withCtx, createElementVNode, Fragment, renderSlot, createCommentVNode } from "vue";
import { Loading } from "../loading/index.mjs";
import { Empty } from "../empty/index.mjs";
import { init } from "echarts";
import { debounce } from "lodash-es";
const _hoisted_1 = { class: "me-chart" };
const _hoisted_2 = { class: "me-chart-wrap" };
const _hoisted_3 = ["id"];
const _hoisted_4 = {
  key: 0,
  class: "me-chart-empty"
};
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "me-chart"
  },
  __name: "chart",
  props: {
    options: { default() {
      return {};
    } },
    loading: { type: Boolean, default: false },
    renderType: { default: "canvas" }
  },
  setup(__props, { expose: __expose }) {
    const props = __props;
    const id = ref((Math.floor(Math.random() * 1e5) + Date.now()).toString(16));
    let instance;
    const handleResize = debounce(() => {
      instance && instance.resize();
    }, 300);
    watch(() => props.options, () => {
      nextTick(() => {
        initEchart();
      });
    });
    onMounted(() => {
      nextTick(() => {
        initEchart();
        window.addEventListener("resize", handleResize);
      });
    });
    onBeforeUnmount(() => {
      window.removeEventListener("resize", handleResize);
    });
    const initEchart = () => {
      instance == null ? void 0 : instance.dispose();
      instance = null;
      if (!instance) {
        const container = document.getElementById(id.value);
        container && (instance = init(container, null, {
          renderer: props.renderType
        }));
      }
      instance == null ? void 0 : instance.clear();
      instance == null ? void 0 : instance.setOption(props.options, true);
    };
    __expose({
      resize: () => {
        instance && instance.resize();
      },
      getInstance() {
        return instance;
      }
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(unref(Loading), {
          tip: "加载中...",
          loading: _ctx.loading
        }, {
          default: withCtx(() => [
            createElementVNode("div", _hoisted_2, [
              Object.keys(_ctx.options).length > 0 ? (openBlock(), createElementBlock("div", {
                key: 0,
                id: id.value,
                class: "me-chart-content"
              }, null, 8, _hoisted_3)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
                !_ctx.loading ? (openBlock(), createElementBlock("div", _hoisted_4, [
                  renderSlot(_ctx.$slots, "empty", {}, () => [
                    createVNode(unref(Empty))
                  ])
                ])) : createCommentVNode("", true)
              ], 64))
            ])
          ]),
          _: 3
        }, 8, ["loading"])
      ]);
    };
  }
});
export {
  _sfc_main as default
};
//# sourceMappingURL=chart.vue2.mjs.map
